package com.example.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

import com.example.main.beans.BarcodeInfoBean;

import java.util.ArrayList;
import java.util.List;

public class SaveView extends View {

    private static final String TAG = "SaveView";

    private Paint mPaint;
    private int myTop = 0;
    private int imageWidth = 700;
    private int itemHeight = 400;
    private List<BarcodeInfoBean> barcodeInfoBeanList = new ArrayList<>();

    public SaveView(
            Context context,
            int imageWidth,
            int itemHeight,
            List<BarcodeInfoBean> barcodeInfoBeanList) {
        super(context);
        this.imageWidth = imageWidth;
        this.itemHeight = itemHeight;
        this.barcodeInfoBeanList = barcodeInfoBeanList;
        init();
    }

    private void init() {
        mPaint = new Paint();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            if (barcodeInfoBeanList.size() > 0) {
                canvas.drawColor(Color.WHITE);
                mPaint.setColor(Color.BLACK);
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setTextSize(60);

                myTop = 0;
                for (int i = 0; i < barcodeInfoBeanList.size(); i++) {

                    String content = barcodeInfoBeanList.get(i).getContent();
                    String name = barcodeInfoBeanList.get(i).getName();
                    Bitmap bitmap = barcodeInfoBeanList.get(i).getBitmap();

                    //名字
                    float widthName = mPaint.measureText(name);
                    int heightName = getTextHeight(mPaint, getBaseLineY(mPaint, myTop));
                    Log.i(TAG, "onDraw: name w:" + widthName + " name h:" + heightName);

                    //内容
                    float widthContent = mPaint.measureText(content);
                    int heightContent = getTextHeight(mPaint, getBaseLineY(mPaint, myTop));
                    Log.i(TAG, "onDraw: content w:" + widthContent + " content h:" + heightContent);

                    int bitmapLeft = (imageWidth - bitmap.getWidth()) / 2;
                    canvas.drawBitmap(bitmap, bitmapLeft, myTop + heightName, mPaint);

                    int nameLeft = (int) ((imageWidth - widthName) / 2);
                    canvas.drawText(name, nameLeft, getBaseLineY(mPaint, myTop), mPaint);

                    int contentLeft = (int) ((imageWidth - widthContent) / 2);
                    canvas.drawText(content, contentLeft, getBaseLineY(mPaint, myTop + heightName + bitmap.getHeight()), mPaint);

                    myTop += itemHeight;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 计算出baseLine位置
     *
     * @param paint
     * @param y
     * @return
     */
    private int getBaseLineY(Paint paint, int y) {
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
        int baseLineY = y - fm.top;
        return baseLineY;
    }

    private int getTextHeight(Paint paint, int baseLineY) {
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
        int top = baseLineY + fm.top;
        int bottom = baseLineY + fm.bottom;
        int height = bottom - top;
        return height;
    }
}
